package com.ruoyi.develop.projectFundBack.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import com.ruoyi.develop.projectFundBack.domain.ProjectFundBack;
import com.ruoyi.develop.projectFundBack.mapper.ProjectFundBackMapper;
import com.ruoyi.develop.projectFundBack.service.IProjectFundBackService;
import com.ruoyi.develop.projectInvoiceInfo.domain.ProjectInvoiceInfo;
import com.ruoyi.develop.projectInvoiceInfo.service.IProjectInvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 项目回款信息管理Service业务层处理
 *
 * @author qh
 * @date 2023-01-10
 */
@Service
public class ProjectFundBackServiceImpl implements IProjectFundBackService {
    @Resource
    private ProjectFundBackMapper projectFundBackMapper;
    @Autowired
    private IProjectBaseInfoService projectBaseInfoService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private IProjectInvoiceInfoService invoiceInfoService;

    /**
     * 查询项目回款信息管理
     *
     * @param id 项目回款信息管理主键
     * @return 项目回款信息管理
     */
    @Override
    public ProjectFundBack selectById(String id) {
        return projectFundBackMapper.selectById(id);
    }

    /**
     * 查询项目回款信息管理列表
     *
     * @param entity 项目回款信息管理
     * @return 项目回款信息管理
     */
    @Override
    public List<ProjectFundBack> selectList(ProjectFundBack entity) {
        return projectFundBackMapper.selectList(entity);
    }

    /**
     * 新增项目回款信息管理
     *
     * @param entity 项目回款信息管理
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int insert(ProjectFundBack entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        int i = projectFundBackMapper.insert(entity);

        if (entity.getBackMoney().compareTo(BigDecimal.ZERO) != 0) {
            ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(entity.getProjectId());
            if (entity.getType().compareTo(BusinessConstants.MONEY_YES) == 0) {
                ProjectFundBack projectFundBack = new ProjectFundBack();
                projectFundBack.setProjectId(entity.getProjectId());
                projectFundBack.setType(BusinessConstants.MONEY_YES);
                List<ProjectFundBack> projectFundBacks = selectList(projectFundBack);
                BigDecimal allMoney = BigDecimal.ZERO;
                for (ProjectFundBack projectFundBackone : projectFundBacks) {
                    if (projectFundBackone.getBackMoney() != null) {
                        allMoney = projectFundBackone.getBackMoney().add(allMoney);
                    }
                }
                GoodsSale goodsSaleEbtity = new GoodsSale();
                goodsSaleEbtity.setProjectId(entity.getProjectId());
                projectBaseInfo.setBackMoney(allMoney);
            }
            projectBaseInfoService.update(projectBaseInfo);
        }

        return i;
    }

    /**
     * 修改项目回款信息管理
     *
     * @param entity 项目回款信息管理
     * @return 结果
     */
    @Override
    public int update(ProjectFundBack entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return projectFundBackMapper.update(entity);
    }

    /**
     * 批量删除项目回款信息管理
     *
     * @param ids 需要删除的项目回款信息管理主键
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int deleteByIds(String[] ids) {
        int i = projectFundBackMapper.deleteByIds(ids);
        //更新对应发票的核销金额和剩余核销金额
        for (String id : ids) {
            ProjectFundBack deleteE = selectById(id);
            String invoiceId = deleteE.getInvoiceId();
            ProjectInvoiceInfo invoiceInfo = invoiceInfoService.selectById(invoiceId);
            invoiceInfo.setRemainMoney(invoiceInfo.getRemainMoney().add(deleteE.getBackMoney()));
            invoiceInfo.setBackMoney(invoiceInfo.getBackMoney().subtract(deleteE.getBackMoney()));
            if (invoiceInfo.getRemainMoney().compareTo(BigDecimal.ZERO) > 0 && invoiceInfo.getBackMoney().compareTo(BigDecimal.ZERO) > 0) {
                invoiceInfo.setStatus(BusinessConstants.FUND_PART);
            } else if (invoiceInfo.getRemainMoney().compareTo(BigDecimal.ZERO) == 0) {
                invoiceInfo.setStatus(BusinessConstants.FUND_YES);
            } else {
                invoiceInfo.setStatus(BusinessConstants.FUND_NO);
            }
            invoiceInfoService.update(invoiceInfo);
        }

        ProjectFundBack entity = selectById(ids[0]);
        String projectId = entity.getProjectId();
        if (entity != null) {
            if (entity.getType().compareTo(BusinessConstants.MONEY_YES) == 0) {
                //计算该项目下所有实际回款总金额
                ProjectFundBack projectFundBack = new ProjectFundBack();
                projectFundBack.setProjectId(projectId);
                projectFundBack.setType(BusinessConstants.MONEY_YES);
                List<ProjectFundBack> projectFundBacks = selectList(projectFundBack);
                BigDecimal allMoney = BigDecimal.ZERO;
                for (ProjectFundBack projectFundBackone : projectFundBacks) {
                    if (projectFundBackone.getBackMoney() != null) {
                        allMoney = projectFundBackone.getBackMoney().add(allMoney);
                    }
                }
                GoodsSale saleNeedUpdate = new GoodsSale();
                saleNeedUpdate.setProjectId(projectId);
                ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(projectId);
                projectBaseInfo.setBackMoney(allMoney);
                projectBaseInfoService.update(projectBaseInfo);
            }
        }
//        }
        return i;
    }

    /**
     * 删除项目回款信息管理信息
     *
     * @param id 项目回款信息管理主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return projectFundBackMapper.deleteById(id);
    }
}
